﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Reflection;
using System.Text;
using ByteSite.DBUtility;
using ByteSite.Common;
using System.Linq.Expressions;
using SqlSugar;

namespace ByteSite.DAL
{
    /// <summary>
    /// 2021.01.15
    /// </summary>
    public class Data
    {
        public SqlSugarClient db;
        private string databaseprefix; //数据库表名前缀
        public Data(string _databaseprefix)
        {
            databaseprefix = _databaseprefix;
            db = new DBUtility.DbContext().Db;
        }
        public Data()
        {
            databaseprefix = string.Empty;
            db = new DBUtility.DbContext().Db;
        }

        /// <summary>
        /// 创建数据库
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public bool CreateDatabase(string name)
        {
            return db.DbMaintenance.CreateDatabase(name);
        }

        /// <summary>
        /// 创建表
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="entityType"></param>
        public void CreateTable(string tableName, Type entityType)
        {
            new SqlSugar.ContextMethods().RemoveCacheAll();
            if (!db.DbMaintenance.IsAnyTable(tableName))
            {
                db.MappingTables.Add(entityType.Name, tableName);
                db.CodeFirst.SetStringDefaultLength(255).InitTables(entityType);
            }
            new SqlSugar.ContextMethods().RemoveCacheAll();
        }

        /// <summary>
        /// 增加列
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="columnInfo"></param>
        public void AddColumn(string tableName, DbColumnInfo columnInfo)
        {
            if (db.DbMaintenance.IsAnyTable(tableName, false))
            {
                if (!db.DbMaintenance.IsAnyColumn(tableName, columnInfo.DbColumnName))
                {
                    if (columnInfo.PropertyType.FullName == typeof(System.String).FullName && columnInfo.Length == 0)
                    {
                        columnInfo.Length = 255;
                    }
                    db.DbMaintenance.AddColumn(tableName, columnInfo);
                    db.DbMaintenance.AddColumnRemark(columnInfo.DbColumnName, tableName, columnInfo.ColumnDescription);
                }
            }

        }

        /// <summary>
        /// 删除表
        /// </summary>
        /// <param name="tableName"></param>
        /// <returns></returns>
        public bool DropTable(string tableName)
        {
            if (db.DbMaintenance.IsAnyTable(tableName))
            {
                return db.DbMaintenance.DropTable(tableName);
            }
            return false;
        }

        /// <summary>
        /// 删除列
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="columnName"></param>
        public bool DropColumn(string tableName, string columnName)
        {
            if (db.DbMaintenance.IsAnyColumn(tableName, columnName))
            {
                return db.DbMaintenance.DropColumn(tableName, columnName);
            }
            return false;
        }

        /// <summary>
        /// 获取所有数据库名称
        /// </summary>
        /// <returns></returns>
        public List<string> DbList()
        {
            return db.DbMaintenance.GetDataBaseList(db);
        }

        /// <summary>
        /// 获取所有表
        /// </summary>
        /// <returns></returns>
        public List<DbTableInfo> TableList()
        {
            return db.DbMaintenance.GetTableInfoList();
        }

        /// <summary>
        /// 获取所有视图
        /// </summary>
        /// <returns></returns>
        public List<DbTableInfo> ViewList()
        {
            return db.DbMaintenance.GetViewInfoList();
        }

        /// <summary>
        /// 获取所有列
        /// </summary>
        /// <param name="tableName"></param>
        /// <returns></returns>
        public List<DbColumnInfo> ColumnList(string tableName)
        {
            return db.DbMaintenance.GetColumnInfosByTableName(tableName);
        }

        /// <summary>
        /// 备份数据库
        /// </summary>
        /// <param name="dbName"></param>
        /// <param name="path"></param>
        /// <returns></returns>
        public bool BackupDb(string dbName, string path)
        {
            return db.DbMaintenance.BackupDataBase(dbName, path);
        }

        /// <summary>
        /// 备份/复制表
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="newTableName"></param>
        /// <returns></returns>
        public bool BackupTable(string tableName,string newTableName)
        {
            return db.DbMaintenance.BackupTable(tableName,newTableName);
        }

        /// <summary>
        /// 修改表名
        /// </summary>
        /// <param name="sourceName"></param>
        /// <param name="distName"></param>
        /// <returns></returns>
        public bool RenameTable(string sourceName, string distName)
        {
            return db.DbMaintenance.RenameTable(sourceName, distName);
        }

        /// <summary>
        /// 修改列名称
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="sourceName"></param>
        /// <param name="distName"></param>
        /// <returns></returns>
        public bool RenameColumn(string tableName ,string sourceName, string distName)
        {
            return db.DbMaintenance.RenameColumn(tableName, sourceName, distName);
        }

        /// <summary>
        /// C#类型 转为 数据库类型
        /// </summary>
        /// <param name="cSharpType"></param>
        /// <returns></returns>
        public string GetDbTypeName(string cSharpType)
        {
            return db.Ado.DbBind.GetDbTypeName(cSharpType);
        }

        /// <summary>
        /// 事务开始
        /// </summary>
        public void BeginTran()
        {
            db.BeginTran();
        }

        /// <summary>
        /// 事务提交
        /// </summary>
        public void CommitTran()
        {
            db.CommitTran();
        }

        /// <summary>
        /// 事务回退
        /// </summary>
        public void RollbackTran()
        {
            db.RollbackTran();
        }

        /// <summary>
        /// 执行sql
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public int SqlExecuteCommand(string sql, params SugarParameter[] parameters)
        {
            return db.Ado.ExecuteCommand(sql, parameters);
        }
        /// <summary>
        /// 执行sql
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public int SqlExecuteCommand(string sql, List<SugarParameter> parameters)
        {
            return db.Ado.ExecuteCommand(sql, parameters);
        }

        /// <summary>
        /// sql查询
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sql"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public T SqlQuerySingle<T>(string sql, List<SugarParameter> parameters)
        {
            return db.Ado.SqlQuerySingle<T>(sql, parameters);
        }

        /// <summary>
        /// sql查询
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sql"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public T SqlQuerySingle<T>(string sql, params SugarParameter[] parameters)
        {
            return db.Ado.SqlQuerySingle<T>(sql, parameters);
        }

        /// <summary>
        /// sql查询
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sql"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public List<T> SqlQuery<T>(string sql, params SugarParameter[] parameters)
        {
            return db.Ado.SqlQuery<T>(sql, parameters);
        }

        /// <summary>
        /// sql查询
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sql"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public List<T> SqlQuery<T>(string sql, List<SugarParameter> parameters)
        {
            return db.Ado.SqlQuery<T>(sql, parameters);
        }

        /// <summary>
        /// sql查询
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sql"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public object SqlScalar(string sql, params SugarParameter[] parameters)
        {
            return db.Ado.GetScalar(sql, parameters);
        }

        /// <summary>
        /// sql查询
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sql"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public object SqlScalar(string sql, List<SugarParameter> parameters)
        {
            return db.Ado.GetScalar(sql, parameters);
        }

        /// <summary>
        /// SQL查询实体
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sql"></param>
        /// <param name="expression"></param>
        /// <param name="strWhere"></param>
        /// <param name="paras"></param>
        /// <param name="orderExpression"></param>
        /// <param name="type"></param>
        /// <param name="strOrder"></param>
        /// <returns></returns>
        public T SqlGet<T>(string sql, Expression<Func<T, bool>> expression, string strWhere, object paras, Expression<Func<T, object>> orderExpression, OrderByType type, string strOrder) where T : class, new()
        {
            return db.SqlQueryable<T>(sql).Where(expression).Where(strWhere, paras).OrderBy(orderExpression, type).OrderBy(strOrder).First();
        }

        /// <summary>
        /// SQL查询列表
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sql"></param>
        /// <param name="expression"></param>
        /// <param name="strWhere"></param>
        /// <param name="paras"></param>
        /// <param name="orderExpression"></param>
        /// <param name="type"></param>
        /// <param name="strOrder"></param>
        /// <returns></returns>
        public List<T> SqlList<T>(string sql, Expression<Func<T, bool>> expression, string strWhere, object paras, Expression<Func<T, object>> orderExpression, OrderByType type, string strOrder) where T : class, new()
        {
            return db.SqlQueryable<T>(sql).Where(expression).Where(strWhere, paras).OrderBy(orderExpression, type).OrderBy(strOrder).ToList();
        }


        public DataSet SqlDataSet(string sql, params SugarParameter[] parameters)
        {
            return db.Ado.GetDataSetAll(sql, parameters);
        }

        public DataSet SqlDataSet(string sql, List<SugarParameter> parameters)
        {
            return db.Ado.GetDataSetAll(sql, parameters);
        }

        public DataTable SqlDataTable(string sql, params SugarParameter[] parameters)
        {
            return db.Ado.GetDataTable(sql, parameters);
        }

        public DataTable SqlDataTable(string sql, List<SugarParameter> parameters)
        {
            return db.Ado.GetDataTable(sql, parameters);
        }

        /// <summary>
        /// sql查询
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sql"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public object Scalar(string sql, List<SugarParameter> parameters)
        {
            return db.Ado.GetScalar(sql, parameters);
        }


        /// <summary>
        /// 是否存在
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="whereExpression">筛选条件</param>
        /// <returns>返回的结果</returns>
        public bool Exists<T>(Expression<Func<T, bool>> whereExpression)
        {
            if (string.IsNullOrEmpty(databaseprefix))
            {
                return db.Queryable<T>().Where(whereExpression).Any();
            }
            else
            {
                return db.Queryable<T>().AS(databaseprefix + typeof(T).ToString()).Where(whereExpression).Any();
            }
        }

        /// <summary>
        /// 是否存在
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="id">主键值</param>
        /// <returns>返回的结果</returns>
        public bool Exists<T>(int id)
        {
            if (string.IsNullOrEmpty(databaseprefix))
            {
                return db.Queryable<T>().InSingle(id) == null ? false : true;
            }
            else
            {
                return db.Queryable<T>().AS(databaseprefix + typeof(T).ToString()).InSingle(id) == null ? false : true;
            }
        }

        /// <summary>
        /// 数量
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="whereExpression">筛选条件</param>
        /// <returns>数量</returns>
        public int Count<T>(Expression<Func<T, bool>> whereExpression)
        {
            if (string.IsNullOrEmpty(databaseprefix))
            {
                return db.Queryable<T>().Where(whereExpression).Count();
            }
            else
            {
                return db.Queryable<T>().AS(databaseprefix + typeof(T).ToString()).Where(whereExpression).Count();
            }
        }

        public int Count<T>(string strWhere)
        {
            if (string.IsNullOrEmpty(databaseprefix))
            {
                return db.Queryable<T>().Where(strWhere).Count();
            }
            else
            {
                return db.Queryable<T>().AS(databaseprefix + typeof(T).ToString()).Where(strWhere).Count();
            }
        }

        /// <summary>
        /// 合计
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="whereExpression">筛选条件</param>
        /// <returns>合计数量</returns>
        public object Sum<T>(Expression<Func<T, bool>> whereExpression, Expression<Func<T, object>> expression)
        {
            if (string.IsNullOrEmpty(databaseprefix))
            {
                return db.Queryable<T>().Where(whereExpression).Sum(expression);
            }
            else
            {
                return db.Queryable<T>().AS(databaseprefix + typeof(T).ToString()).Where(whereExpression).Sum(expression);
            }
        }

        /// <summary>
        /// 添加数据
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="model">数据</param>
        /// <returns>传入实体添加自增列</returns>
        public int Insert<T>(T model) where T : class, new()
        {
            if (string.IsNullOrEmpty(databaseprefix))
            {
                return db.Insertable<T>(model).ExecuteReturnIdentity();
            }
            else
            {
                return db.Insertable<T>(model).AS(databaseprefix + typeof(T).ToString()).ExecuteReturnIdentity();
            }
        }

        /// <summary>
        /// 添加数据
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="list">数据集合</param>
        /// <returns>影响的行数</returns>
        public int Insert<T>(List<T> list) where T : class, new()
        {
            if (string.IsNullOrEmpty(databaseprefix))
            {
                return db.Insertable<T>(list).ExecuteCommand();
            }
            else
            {
                return db.Insertable<T>(list).AS(databaseprefix + typeof(T).ToString()).ExecuteCommand();
            }
        }

        /// <summary>
        /// 更新数据
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="model">实体</param>
        /// <returns>结果</returns>
        public bool Update<T>(T model) where T : class, new()
        {
            if (string.IsNullOrEmpty(databaseprefix))
            {
                return db.Updateable<T>(model).ExecuteCommand() > 0;
            }
            else
            {
                return db.Updateable<T>(model).AS(databaseprefix + typeof(T).ToString()).ExecuteCommand() > 0;
            }
        }

        /// <summary>
        /// 更新数据
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="list">数据集合</param>
        /// <returns>影响的行数</returns>
        public int Update<T>(List<T> list) where T : class, new()
        {
            if (string.IsNullOrEmpty(databaseprefix))
            {
                return db.Updateable<T>(list).ExecuteCommand();
            }
            else
            {
                return db.Updateable<T>(list).AS(databaseprefix + typeof(T).ToString()).ExecuteCommand();
            }
        }

        /// <summary>
        /// 更新字段
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="expression">it => it.Num== it.Num+1</param>
        /// <param name="whereExpression">条件筛选</param>
        /// <returns>执行结果</returns>
        public bool UpdateColumns<T>(Expression<Func<T, bool>> expression, Expression<Func<T, bool>> whereExpression) where T : class, new()
        {
            if (string.IsNullOrEmpty(databaseprefix))
            {
                return db.Updateable<T>().SetColumns(expression).Where(whereExpression).ExecuteCommand() > 0;
            }
            else
            {
                return db.Updateable<T>().AS(databaseprefix + typeof(T).ToString()).SetColumns(expression).Where(whereExpression).ExecuteCommand() > 0;
            }
        }

        /// <summary>
        /// 更新字段
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="expression">it => it.Num== it.Num+1</param>
        /// <param name="whereExpression">条件筛选</param>
        /// <returns>执行结果</returns>
        public bool UpdateColumns<T>(Expression<Func<T, T>> expression, Expression<Func<T, bool>> whereExpression) where T : class, new()
        {
            if (string.IsNullOrEmpty(databaseprefix))
            {
                return db.Updateable<T>().SetColumns(expression).Where(whereExpression).ExecuteCommand() > 0;
            }
            else
            {
                return db.Updateable<T>().AS(databaseprefix + typeof(T).ToString()).SetColumns(expression).Where(whereExpression).ExecuteCommand() > 0;
            }
        }

        /// <summary>
        /// 删除数据
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="whereExpression">筛选条件</param>
        /// <returns></returns>
        public int Delete<T>(Expression<Func<T, bool>> whereExpression) where T : class, new()
        {
            if (string.IsNullOrEmpty(databaseprefix))
            {
                return db.Deleteable<T>(whereExpression).ExecuteCommand();
            }
            else
            {
                return db.Deleteable<T>(whereExpression).AS(databaseprefix + typeof(T).ToString()).ExecuteCommand();
            }
        }

        /// <summary>
        /// 删除数据
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="id">主键</param>
        /// <returns>返回结果</returns>
        public bool Delete<T>(int id) where T : class, new()
        {
            if (string.IsNullOrEmpty(databaseprefix))
            {
                T model = Get<T>(id);
                if (model != null)
                    return db.Deleteable<T>(model).ExecuteCommand() > 0;
                else
                    return false;
            }
            else
            {
                T model = Get<T>(id);
                if (model != null)
                    return db.Deleteable<T>(model).AS(databaseprefix + typeof(T).ToString()).ExecuteCommand() > 0;
                else
                    return false;
            }
        }

        /// <summary>
        /// 获取数据
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="whereExpression">筛选条件</param>
        /// <returns>数据</returns>
        public T Get<T>(Expression<Func<T, bool>> whereExpression)
        {
            T model;
            if (string.IsNullOrEmpty(databaseprefix))
            {
                model = db.Queryable<T>().Where(whereExpression).First();
            }
            else
            {
                model = db.Queryable<T>().AS(databaseprefix + typeof(T).ToString()).Where(whereExpression).First();
            }
            return model;
        }

        /// <summary>
        /// 获取数据
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="id">主键值</param>
        /// <returns>数据</returns>
        public T Get<T>(int id)
        {
            T model;
            if (string.IsNullOrEmpty(databaseprefix))
            {
                model = db.Queryable<T>().InSingle(id);
            }
            else
            {
                model = db.Queryable<T>().AS(databaseprefix + typeof(T).ToString()).InSingle(id);
            }
            return model;
        }

        /// <summary>
        /// 获取数据
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="whereExpression">筛选条件</param>
        /// <param name="orderExpression">排序条件</param>
        /// <returns>数据</returns>
        public T Get<T>(Expression<Func<T, bool>> whereExpression, Expression<Func<T, object>> orderExpression)
        {
            T model;
            if (string.IsNullOrEmpty(databaseprefix))
            {
                model = db.Queryable<T>().Where(whereExpression).OrderBy(orderExpression).First();
            }
            else
            {
                model = db.Queryable<T>().AS(databaseprefix + typeof(T).ToString()).Where(whereExpression).OrderBy(orderExpression).First();
            }
            return model;
        }

        /// <summary>
        /// 获取数据
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="whereExpression">筛选条件</param>
        /// <param name="orderExpression">排序条件</param>
        /// <param name="orderByType">排序类型</param>
        /// <returns>数据</returns>
        public T Get<T>(Expression<Func<T, bool>> whereExpression, Expression<Func<T, object>> orderExpression,SqlSugar.OrderByType orderByType)
        {
            T model;
            if (string.IsNullOrEmpty(databaseprefix))
            {
                model = db.Queryable<T>().Where(whereExpression).OrderBy(orderExpression, orderByType).First();
            }
            else
            {
                model = db.Queryable<T>().AS(databaseprefix + typeof(T).ToString()).Where(whereExpression).OrderBy(orderExpression, orderByType).First();
            }
            return model;
        }

        /// <summary>
        /// 获取数据集合
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="whereExpression">筛选条件</param>
        /// <returns>数据集</returns>
        public List<T> List<T>(Expression<Func<T, bool>> whereExpression)
        {
            if (string.IsNullOrEmpty(databaseprefix))
            {
                return db.Queryable<T>().Where(whereExpression).ToList();
            }
            else
            {
                return db.Queryable<T>().AS(databaseprefix + typeof(T).ToString()).Where(whereExpression).ToList();
            }
        }

        /// <summary>
        /// 获取数据集合
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="whereExpression">筛选条件</param>
        /// <param name="strWhere">其他筛选条件</param>
        /// <returns>数据集</returns>
        public List<T> List<T>(Expression<Func<T, bool>> whereExpression,string strWhere)
        {
            if (string.IsNullOrEmpty(databaseprefix))
            {
                return db.Queryable<T>().Where(whereExpression).Where(strWhere).ToList();
            }
            else
            {
                return db.Queryable<T>().AS(databaseprefix + typeof(T).ToString()).Where(whereExpression).Where(strWhere).ToList();
            }
        }

        /// <summary>
        /// 获取数据集合
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="strWhere">筛选条件</param>
        /// <returns>数据集</returns>
        public List<T> List<T>(string strWhere)
        {
            if (string.IsNullOrEmpty(databaseprefix))
            {
                return db.Queryable<T>().Where(strWhere).ToList();
            }
            else
            {
                return db.Queryable<T>().AS(databaseprefix + typeof(T).ToString()).Where(strWhere).ToList();
            }
        }

        /// <summary>
        /// 获取数据集合
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="whereExpression">筛选条件</param>
        /// <param name="orderExpression">排序条件</param>
        /// <returns>数据集</returns>
        public List<T> List<T>(Expression<Func<T, bool>> whereExpression, Expression<Func<T, object>> orderExpression)
        {
            if (string.IsNullOrEmpty(databaseprefix))
            {
                return db.Queryable<T>().Where(whereExpression).OrderBy(orderExpression).ToList();
            }
            else
            {
                return db.Queryable<T>().AS(databaseprefix + typeof(T).ToString()).Where(whereExpression).OrderBy(orderExpression).ToList();
            }
        }

        /// <summary>
        /// 获取数据集合
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="whereExpression">筛选条件</param>
        /// <param name="strWhere">其他筛选条件</param>
        /// <param name="orderExpression">排序条件</param>
        /// <param name="orderByType">排序类型</param>
        /// <returns>数据集</returns>
        public List<T> List<T>(Expression<Func<T, bool>> whereExpression, string strWhere, Expression<Func<T, object>> orderExpression, SqlSugar.OrderByType orderByType)
        {
            if (string.IsNullOrEmpty(databaseprefix))
            {
                return db.Queryable<T>().Where(whereExpression).Where(strWhere).OrderBy(orderExpression, orderByType).ToList();
            }
            else
            {
                return db.Queryable<T>().AS(databaseprefix + typeof(T).ToString()).Where(whereExpression).Where(strWhere).OrderBy(orderExpression, orderByType).ToList();
            }
        }

        /// <summary>
        /// 获取数据集合
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="whereExpression">筛选条件</param>
        /// <param name="orderExpression">排序条件</param>
        /// <param name="orderByType">排序类型</param>
        /// <returns>数据集</returns>
        public List<T> List<T>(Expression<Func<T, bool>> whereExpression, Expression<Func<T, object>> orderExpression,SqlSugar.OrderByType orderByType)
        {
            if (string.IsNullOrEmpty(databaseprefix))
            {
                return db.Queryable<T>().Where(whereExpression).OrderBy(orderExpression,orderByType).ToList();
            }
            else
            {
                return db.Queryable<T>().AS(databaseprefix + typeof(T).ToString()).Where(whereExpression).OrderBy(orderExpression,orderByType).ToList();
            }
        }

        /// <summary>
        /// 获取数据集合
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="strWhere">筛选条件</param>
        /// <param name="strOrder">排序条件</param>
        /// <returns>数据集</returns>
        public List<T> List<T>(string strWhere, string strOrder)
        {
            if (string.IsNullOrEmpty(databaseprefix))
            {
                return db.Queryable<T>().Where(strWhere).OrderBy(strOrder).ToList();
            }
            else
            {
                return db.Queryable<T>().AS(databaseprefix + typeof(T).ToString()).Where(strWhere).OrderBy(strOrder).ToList();
            }
        }

        /// <summary>
        /// 获取数据集合
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="top">前N条</param>
        /// <param name="whereExpression">筛选条件</param>
        /// <param name="orderExpression">排序条件</param>
        /// <returns>数据集</returns>
        public List<T> List<T>(int top, Expression<Func<T, bool>> whereExpression, Expression<Func<T, object>> orderExpression)
        {
            if (top == 0)
            {
                if (string.IsNullOrEmpty(databaseprefix))
                {
                    return db.Queryable<T>().Where(whereExpression).OrderBy(orderExpression).ToList();
                }
                else
                {
                    return db.Queryable<T>().AS(databaseprefix + typeof(T).ToString()).Where(whereExpression).OrderBy(orderExpression).ToList();
                }
            }
            else
            {
                if (string.IsNullOrEmpty(databaseprefix))
                {
                    return db.Queryable<T>().Take(top).Where(whereExpression).OrderBy(orderExpression).ToList();
                }
                else
                {
                    return db.Queryable<T>().AS(databaseprefix + typeof(T).ToString()).Take(top).Where(whereExpression).OrderBy(orderExpression).ToList();
                }
            }
        }

        /// <summary>
        /// 获取数据集合
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="top">前N条</param>
        /// <param name="whereExpression">筛选条件</param>
        /// <param name="orderExpression">排序条件</param>
        /// <returns>数据集</returns>
        public List<T> List<T>(int top, Expression<Func<T, bool>> whereExpression, string strOrder)
        {
            if (top == 0)
            {
                if (string.IsNullOrEmpty(databaseprefix))
                {
                    return db.Queryable<T>().Where(whereExpression).OrderBy(strOrder).ToList();
                }
                else
                {
                    return db.Queryable<T>().AS(databaseprefix + typeof(T).ToString()).Where(whereExpression).OrderBy(strOrder).ToList();
                }
            }
            else
            {
                if (string.IsNullOrEmpty(databaseprefix))
                {
                    return db.Queryable<T>().Take(top).Where(whereExpression).OrderBy(strOrder).ToList();
                }
                else
                {
                    return db.Queryable<T>().AS(databaseprefix + typeof(T).ToString()).Take(top).Where(whereExpression).OrderBy(strOrder).ToList();
                }
            }
        }

        /// <summary>
        /// 获取数据集合
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="top">前N条</param>
        /// <param name="whereExpression">筛选条件</param>
        /// <param name="orderExpression">排序条件</param>
        /// <param name="orderByType">排序类型</param>
        /// <returns>数据集</returns>
        public List<T> List<T>(int top, Expression<Func<T, bool>> whereExpression, Expression<Func<T, object>> orderExpression, SqlSugar.OrderByType orderByType)
        {
            if (top == 0)
            {
                if (string.IsNullOrEmpty(databaseprefix))
                {
                    return db.Queryable<T>().Where(whereExpression).OrderBy(orderExpression, orderByType).ToList();
                }
                else
                {
                    return db.Queryable<T>().AS(databaseprefix + typeof(T).ToString()).Where(whereExpression).OrderBy(orderExpression, orderByType).ToList();
                }
            }
            else
            {
                if (string.IsNullOrEmpty(databaseprefix))
                {
                    return db.Queryable<T>().Take(top).Where(whereExpression).OrderBy(orderExpression, orderByType).ToList();
                }
                else
                {
                    return db.Queryable<T>().AS(databaseprefix + typeof(T).ToString()).Take(top).Where(whereExpression).OrderBy(orderExpression, orderByType).ToList();
                }

            }
        }

        /// <summary>
        /// 获取数据集合
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="top">前N条</param>
        /// <param name="whereExpression">筛选条件</param>
        /// <param name="strWhere">其他筛选条件</param>
        /// <param name="orderExpression">排序条件</param>
        /// <returns>数据集</returns>
        public List<T> List<T>(int top, Expression<Func<T, bool>> whereExpression, string strWhere, Expression<Func<T, object>> orderExpression)
        {
            if (top == 0)
            {
                if (string.IsNullOrEmpty(databaseprefix))
                {
                    return db.Queryable<T>().Where(whereExpression).Where(strWhere).OrderBy(orderExpression).ToList();
                }
                else
                {
                    return db.Queryable<T>().AS(databaseprefix + typeof(T).ToString()).Where(whereExpression).Where(strWhere).OrderBy(orderExpression).ToList();
                }
            }
            else
            {
                if (string.IsNullOrEmpty(databaseprefix))
                {
                    return db.Queryable<T>().Take(top).Where(whereExpression).Where(strWhere).OrderBy(orderExpression).ToList();
                }
                else
                {
                    return db.Queryable<T>().AS(databaseprefix + typeof(T).ToString()).Take(top).Where(whereExpression).Where(strWhere).OrderBy(orderExpression).ToList();
                }
            }
        }

        /// <summary>
        /// 获取数据集合
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="top">前N条</param>
        /// <param name="whereExpression">筛选条件</param>
        /// <param name="strWhere">其他筛选条件</param>
        /// <param name="orderExpression">排序条件</param>
        /// <param name="orderByType">排序类型</param>
        /// <returns>数据集</returns>
        public List<T> List<T>(int top, Expression<Func<T, bool>> whereExpression, string strWhere, Expression<Func<T, object>> orderExpression,SqlSugar.OrderByType orderByType)
        {
            if (top == 0)
            {
                if (string.IsNullOrEmpty(databaseprefix))
                {
                    return db.Queryable<T>().Where(whereExpression).Where(strWhere).OrderBy(orderExpression, orderByType).ToList();
                }
                else
                {
                    return db.Queryable<T>().AS(databaseprefix + typeof(T).ToString()).Where(whereExpression).Where(strWhere).OrderBy(orderExpression, orderByType).ToList();
                }
            }
            else
            {
                if (string.IsNullOrEmpty(databaseprefix))
                {
                    return db.Queryable<T>().Take(top).Where(whereExpression).Where(strWhere).OrderBy(orderExpression, orderByType).ToList();
                }
                else
                {
                    return db.Queryable<T>().AS(databaseprefix + typeof(T).ToString()).Take(top).Where(whereExpression).Where(strWhere).OrderBy(orderExpression, orderByType).ToList();
                }
            }
        }

        /// <summary>
        /// 获取数据集合
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="top">前N条</param>
        /// <param name="strWhere">筛选条件</param>
        /// <param name="strOrder">排序条件</param>
        /// <returns>数据集</returns>
        public List<T> List<T>(int top, string strWhere, string strOrder)
        {

            if (top == 0)
            {
                if (string.IsNullOrEmpty(databaseprefix))
                {
                    return db.Queryable<T>().Where(strWhere).OrderBy(strOrder).ToList();
                }
                else
                {
                    return db.Queryable<T>().AS(databaseprefix + typeof(T).ToString()).Where(strWhere).OrderBy(strOrder).ToList();
                }
            }
            else
            {
                if (string.IsNullOrEmpty(databaseprefix))
                {
                    return db.Queryable<T>().Take(top).Where(strWhere).OrderBy(strOrder).ToList();
                }
                else
                {
                    return db.Queryable<T>().AS(databaseprefix + typeof(T).ToString()).Take(top).Where(strWhere).OrderBy(strOrder).ToList();
                }
            }
        }

        /// <summary>
        /// 获取数据集合
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="pageSize">每页数量</param>
        /// <param name="pageIndex">当前页码</param>
        /// <param name="whereExpression">筛选条件</param>
        /// <param name="orderExpression">排序条件</param>
        /// <param name="recordCount">返回数量</param>
        /// <returns>数据集</returns>
        public List<T> List<T>(int pageSize, int pageIndex, Expression<Func<T, bool>> whereExpression, Expression<Func<T, object>> orderExpression, out int recordCount)
        {
            recordCount = 0;
            if (string.IsNullOrEmpty(databaseprefix))
            {
                return db.Queryable<T>().Where(whereExpression).OrderBy(orderExpression).ToPageList(pageIndex, pageSize, ref recordCount);
            }
            else
            {
                return db.Queryable<T>().AS(databaseprefix + typeof(T).ToString()).Where(whereExpression).OrderBy(orderExpression).ToPageList(pageIndex, pageSize, ref recordCount);
            }
        }

        public List<T> List<T>(int pageSize, int pageIndex, Expression<Func<T, bool>> whereExpression, string strOrder, out int recordCount)
        {
            recordCount = 0;
            if (string.IsNullOrEmpty(databaseprefix))
            {
                return db.Queryable<T>().Where(whereExpression).OrderBy(strOrder).ToPageList(pageIndex, pageSize, ref recordCount);
            }
            else
            {
                return db.Queryable<T>().AS(databaseprefix + typeof(T).ToString()).Where(whereExpression).OrderBy(strOrder).ToPageList(pageIndex, pageSize, ref recordCount);
            }
        }

        /// <summary>
        /// 获取数据集合
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="pageSize">每页数量</param>
        /// <param name="pageIndex">当前页码</param>
        /// <param name="whereExpression">筛选条件</param>
        /// <param name="orderExpression">排序条件</param>
        /// <param name="orderByType">排序类型</param>
        /// <param name="recordCount">返回数量</param>
        /// <returns>数据集</returns>
        public List<T> List<T>(int pageSize, int pageIndex, Expression<Func<T, bool>> whereExpression, Expression<Func<T, object>> orderExpression,SqlSugar.OrderByType orderByType, out int recordCount)
        {
            recordCount = 0;
            if (string.IsNullOrEmpty(databaseprefix))
            {
                return db.Queryable<T>().Where(whereExpression).OrderBy(orderExpression, orderByType).ToPageList(pageIndex, pageSize, ref recordCount);
            }
            else
            {
                return db.Queryable<T>().AS(databaseprefix + typeof(T).ToString()).Where(whereExpression).OrderBy(orderExpression, orderByType).ToPageList(pageIndex, pageSize, ref recordCount);
            }
        }

        /// <summary>
        /// 获取数据集合
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="pageSize">每页数量</param>
        /// <param name="pageIndex">当前页码</param>
        /// <param name="whereExpression">筛选条件</param>
        /// <param name="strWhere">其他筛选条件</param>
        /// <param name="orderExpression">排序条件</param>
        /// <param name="recordCount">返回数量</param>
        /// <returns>数据集</returns>
        public List<T> List<T>(int pageSize, int pageIndex, Expression<Func<T, bool>> whereExpression, string strWhere, Expression<Func<T, object>> orderExpression, out int recordCount)
        {
            recordCount = 0;
            if (string.IsNullOrEmpty(databaseprefix))
            {
                return db.Queryable<T>().Where(whereExpression).Where(strWhere).OrderBy(orderExpression).ToPageList(pageIndex, pageSize, ref recordCount);
            }
            else
            {
                return db.Queryable<T>().AS(databaseprefix + typeof(T).ToString()).Where(whereExpression).Where(strWhere).OrderBy(orderExpression).ToPageList(pageIndex, pageSize, ref recordCount);
            }
        }

        /// <summary>
        /// 获取数据集合
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="pageSize">每页数量</param>
        /// <param name="pageIndex">当前页码</param>
        /// <param name="whereExpression">筛选条件</param>
        /// <param name="strWhere">其他筛选条件</param>
        /// <param name="orderExpression">排序条件</param>
        /// <param name="orderByType">排序类型</param>
        /// <param name="recordCount">返回数量</param>
        /// <returns>数据集</returns>
        public List<T> List<T>(int pageSize, int pageIndex, Expression<Func<T, bool>> whereExpression, string strWhere, Expression<Func<T, object>> orderExpression,SqlSugar.OrderByType orderByType, out int recordCount)
        {
            recordCount = 0;
            if (string.IsNullOrEmpty(databaseprefix))
            {
                return db.Queryable<T>().Where(whereExpression).Where(strWhere).OrderBy(orderExpression, orderByType).ToPageList(pageIndex, pageSize, ref recordCount);
            }
            else
            {
                return db.Queryable<T>().AS(databaseprefix + typeof(T).ToString()).Where(whereExpression).Where(strWhere).OrderBy(orderExpression, orderByType).ToPageList(pageIndex, pageSize, ref recordCount);
            }
        }

        /// <summary>
        /// 获取数据集合
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="pageSize">每页数量</param>
        /// <param name="pageIndex">当前页码</param>
        /// <param name="strWhere">筛选条件</param>
        /// <param name="strOrder">排序条件</param>
        /// <param name="recordCount">返回数量</param>
        /// <returns>数据集</returns>
        public List<T> List<T>(int pageSize, int pageIndex, string strWhere, string strOrder, out int recordCount)
        {
            recordCount = 0;
            if (string.IsNullOrEmpty(databaseprefix))
            {
                return db.Queryable<T>().Where(strWhere).OrderBy(strOrder).ToPageList(pageIndex, pageSize, ref recordCount);
            }
            else
            {
                return db.Queryable<T>().AS(databaseprefix + typeof(T).ToString()).Where(strWhere).OrderBy(strOrder).ToPageList(pageIndex, pageSize, ref recordCount);
            }
        }
        
    }
}